/**
 * Created by Arnald on 2017/4/21.
 */
let net = require('net');
let msg = require('../util/msg.js');
let log = require('../util/log.js');
let action = require('./action.js');
let cron = require('./cron.js');
let gameData = require('../util/gameData.js');
let util = require('../util/util.js');
let rpc = require('../util/rpc.js');

let argv = process.argv.splice(2);
gameData.init();
util.setServerInfo('game', argv[0]);

//app.listen(util.getServerInfo('http'), function () {
//    log.p('游戏服务器HTTP:' + util.getServerInfo('http'));
//});

net.createServer(function (c) { // Connection监听器
    let cid = util.cid(c);
    msg.addLink(cid, c);
    c.on("end", function () {
        log.p('游戏服务器连接断开');
    });
    c.on("error", function (e) {
        log.e('游戏服务器连接错误:' + e);
    });
    c.on('data', function (message) {
        let list = util.messageToArr(message, cid);
        for (let i in list) {
            action(list[i], cid);
        }
    });
}).listen(util.getServerInfo('port'), function () {
    // Listening监听器
    log.p("游戏服务器启动:" + util.getServerInfo('port'));
    cron.init();
    rpc.dc.connect('gameId:' + util.getServerInfo('id'));
    rpc.match.connect('gameId:' + util.getServerInfo('id'));
    //rpc.arena.connect('gameId:' + util.getServerInfo('id'));
});